﻿using System;
using System.Security.Principal;

namespace AgileBoard.Web.Controllers
{
    public class ActiveUser : IPrincipal
    {
        public static ActiveUser Guest
        {
            get { return new ActiveUser {Email = "Guest@guest.com", Role = Role.Guest}; }
        }

        public string PublicIdentity
        {
            get
            {
                if (CanLogin) return Email;
                return "Guest";
            }
        }

        public bool CanLogin
        {
            get { return IsAdministrator || IsLeadDeveoper; }
        }

        public bool IsAdministrator
        {
            get { return Role.RoleID == Role.AdministratorId; }
        }

        public bool IsLeadDeveoper
        {
            get { return Role.RoleID == Role.LeadDeveloperId; }
        }

        public bool IsDeveloper
        {
            get { return Role.RoleID == Role.DeveloperId; }
        }

        /// <summary>
        /// Email地址
        /// </summary>
        public String Email { get; set; }

        /// <summary>
        /// User role
        /// </summary>
        /// 
        public Role Role { get; set; }

        /// <summary>
        /// 手机号码
        /// </summary>
        public String Mobile { get; set; }

        /// <summary>
        /// 用户密码
        /// </summary>
        public String Password { get; set; }

        /// <summary>
        /// 电话号码
        /// </summary>
        public String Telephone { get; set; }

        /// <summary>
        /// 用户名称
        /// </summary>
        public String UserName { get; set; }

        #region IPrincipal Members

        public IIdentity Identity
        {
            get
            {
                bool isAuthenticated = !(Role.Name == Role.Guest.Name);
                return new Identity(isAuthenticated, Email);
            }
        }

        public bool IsInRole(string role)
        {
            return Role.Name == role;
        }

        #endregion
    }

    /// <summary>
    /// Simple implementation of IIdentity
    /// </summary>
    public class Identity : IIdentity
    {
        private readonly bool isAuthenticated;
        private readonly string name;

        public Identity(bool isAuthenticated, string name)
        {
            this.isAuthenticated = isAuthenticated;
            this.name = name;
        }

        #region IIdentity Members

        public string AuthenticationType
        {
            get { return "Forms"; }
        }

        public bool IsAuthenticated
        {
            get { return isAuthenticated; }
        }

        public string Name
        {
            get { return name; }
        }

        #endregion
    }
}